package yz.dut.edu.graduate.controller;


import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yz.dut.edu.graduate.bean.Result;
import yz.dut.edu.graduate.pojo.Dish;
import yz.dut.edu.graduate.service.ComprehensiveService2;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/customer")
public class DishCustomerController {

    @Autowired
    ComprehensiveService2 comprehensiveService2;

    @PostMapping("/order")
    public Result DishOrder(@RequestBody Map<String,Object> map){
        Map<String,Integer> dishs=(Map<String,Integer>)map.get("dishs");
        String position=(String)map.get("position");
        String remark=(String)map.get("remark");
        Object temp=map.get("order_price");
        float order_price;
        if("java.lang.Integer".equals(temp.getClass().getName())){
            order_price=((Integer)map.get("order_price")).floatValue();
        }else if("java.lang.Double".equals(temp.getClass().getName())){
            order_price=((Double)map.get("order_price")).floatValue();
        }else{
            return new Result();
        }
        Result result=comprehensiveService2.BuildHistory(dishs,position,remark,order_price);
        return result;
    }

    @PostMapping("/affirm")
    public Result AffirmCheck(@RequestBody Map<String,Object> map){
        long h_id=Long.parseLong((String)map.get("h_id"));
        float real_price=Float.parseFloat((String)map.get("real_price"));
        return comprehensiveService2.AffirmCheck(h_id,real_price);
    }

    @GetMapping("/showPage")
    public PageInfo SelectPage(HttpServletRequest request){
        int pageNum= Integer.parseInt(request.getParameter("pagenum"));
        int pageSize=Integer.parseInt(request.getParameter("pagesize"));
        return comprehensiveService2.SelectPage(pageNum,pageSize);
    }

    @GetMapping("/getDetailsByHid")
    public Result SelectHistoryDetailsByHid(HttpServletRequest request){
        long h_id= Long.parseLong(request.getParameter("hId"));
        return comprehensiveService2.SelectHistoryDetailsByHid(h_id);
    }

    @PostMapping("/searchRange")
    public Result SearchRange(HttpServletRequest request){
        String start=request.getParameter("start");
        String end=request.getParameter("end");
        return comprehensiveService2.SearchRange(start,end);
    }

}
